package com.sdu.douban_comment.utils;


import com.alibaba.fastjson2.JSONObject;
import com.alibaba.fastjson2.JSON;
import com.sdu.douban_comment.entity.Comment;
import com.sdu.douban_comment.entity.Film;
import com.sdu.douban_comment.entity.ShortComment;
import com.sdu.douban_comment.entity.StarPercentage;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.StringUtils;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class DataUtils {

    private final static Logger logger= LoggerFactory.getLogger(DataUtils.class);
    private static String jsonPath="C:\\Users\\任晓飞\\Desktop\\movies.json";

    public static String[] strip(String[] strs){
        String[] res = new String[strs.length];
        for (int i = 0; i < strs.length; i++) {
            res[i]=strs[i].strip();
        }
        return res;
    }


    public static List<Film> convert() throws IOException {
        BufferedReader br = new BufferedReader(new FileReader(new File(jsonPath)));
        List<JSONObject> jo = JSON.parseArray(br.readLine(), JSONObject.class);
        br.close();
        List<Film> films = new ArrayList<Film>();

        String fileName;
        String[] director;
        String[] screenWrite;
        String[] actors;
        String[] type;
        String[] productionCty;
        String[] language;
        String releaseDate;

        String duration;
        String[] relateFilms;
        double score;
        long evaluatorNum;


        for(JSONObject jobj:jo) {
            String releaseDataFormat;
            Film film = new Film();
            //基础信息部分
            fileName = jobj.getString("电影名");
            logger.info("正在转换的电影名: {}",fileName);
            relateFilms=jobj.getString("相关电影").split("/");
            JSONObject baseInfo = jobj.getJSONObject("基本信息");
            director=baseInfo.getString("导演").split("/");
            screenWrite=baseInfo.getString("编剧").split("/");
            actors=baseInfo.getString("主演").split("/");
            type=baseInfo.getString("类型").split("/");
            productionCty=baseInfo.getString("制片国家").split("/");
            language=baseInfo.getString("语言").split("/");
            releaseDate=baseInfo.getString("上映日期");

            duration=baseInfo.getString("片长");
            //去除前后空格
            director=strip(director);
            screenWrite=strip(screenWrite);
            actors=strip(actors);
            type=strip(type);
            language=strip(language);
            productionCty=strip(productionCty);
            //处理上映日期，为了方便后续根据日期统计
            releaseDataFormat=releaseDate;
            if(StringUtils.hasLength(releaseDataFormat)){
                if(releaseDataFormat.contains("/")){
                    releaseDataFormat=releaseDataFormat.split("/")[0];
                }
                if(releaseDataFormat.contains("(")){
                    releaseDataFormat=releaseDataFormat.substring(0,releaseDataFormat.indexOf("("));
                }
                if(releaseDataFormat.length()==4){
                    releaseDataFormat=releaseDataFormat+="-00-00";
                }
            }else{
                releaseDataFormat="0000-00-00";
            }

            film.setFileName(fileName);
            film.setRelateFilms(relateFilms);
            film.setDirector(director);
            film.setScreenWriter(screenWrite);
            film.setActors(actors);
            film.setType(type);
            film.setProductionCty(productionCty);
            film.setLanguage(language);
            film.setReleaseDate(releaseDate);
            film.setReleaseDateFormat(releaseDataFormat);
            film.setDuration(duration);

            //豆瓣评分部分
            Comment comment = new Comment();
            StarPercentage starPercentage = new StarPercentage();
            JSONObject douban = jobj.getJSONObject("豆瓣评分");
            score=douban.getDoubleValue("评分");
            evaluatorNum=douban.getLongValue("评价人数");
            JSONObject doubanStar = douban.getJSONObject("星级百分比");
            starPercentage.setFiveStar(Double.parseDouble(doubanStar.getString("5星").split("%")[0]));
            starPercentage.setFourStar(Double.parseDouble(doubanStar.getString("4星").split("%")[0]));
            starPercentage.setThreeStar(Double.parseDouble(doubanStar.getString("3星").split("%")[0]));
            starPercentage.setTwoStar(Double.parseDouble(doubanStar.getString("2星").split("%")[0]));
            starPercentage.setOneStar(Double.parseDouble(doubanStar.getString("1星").split("%")[0]));
            comment.setPercentage(starPercentage);
            comment.setScore(score);
            comment.setEvaluatorNum(evaluatorNum);

            //短评
            JSONObject commentsJ = jobj.getJSONObject("短评");
            ShortComment[] shortComments;

            if(commentsJ.isEmpty()){
                shortComments=null;
            }else{
                shortComments=new ShortComment[100];
                for(int i=1;i<=100;i++){
                    JSONObject obj = commentsJ.getJSONObject(String.valueOf(i));
                    if(obj==null){
                        continue;
                    }
                    ShortComment shortComment = new ShortComment();
                    shortComment.setEvaluator(obj.getString("评价人"));
                    shortComment.setTime(obj.getString("时间"));
                    shortComment.setLocation(obj.getString("所在地"));
                    shortComment.setStar(obj.getString("星级"));
                    shortComment.setContent(obj.getString("内容"));
                    shortComments[i-1]=shortComment;
                }
            }
            comment.setShortComments(shortComments);
            film.setComment(comment);
            films.add(film);
        }
        return films;
    }


}
